<?php

set_time_limit(520);

require dirname(__FILE__).'/../config.php';

require WP_VERSION_PATH . '/library/functions.php';
require WP_VERSION_PATH . '/library/tokenizer.php';
require WP_VERSION_PATH . '/library/version_writer.php';

if(true === USE_SQLITE)
{
	require WP_VERSION_PATH . '/library/sqlite_save.php';
}
else if(true === USE_MYSQL)
{
	require WP_VERSION_PATH . '/library/mysql_save.php';
}

$wordpress = move_wordpress_zip();

// WordPress exists in an archive, so we have to open it
// as an archive and extract the contents.
// The Zip extension makes this available without having to extract
// the archive first. Does it transparently.
$za = new ZipArchive();

// Open the moved upload file to be processed.
$za->open($wordpress);

// We loop through each of the files and open them to check to see
// if they are PHP. We keep a white list of the file name in order
// to do the parsing. We don't want to parse files, which aren't PHP.
$wp_version = new WP_Version_Implementation($_POST['version']);
$wp_version->setup();

//WP_Version_Implementation::$handle->query("DELETE FROM wp_version_info");

$j = 0;
for ($i=0; $i<$za->numFiles;$i++)
{
	// Get the info on the file for checking the filename.
	$file = $za->statIndex($i);

	// Get the extension for checking the file extension.
	$extension = substr($file['name'], -3, 3);

	// We want anything that ends with php and some early files
	// ended with inc, which are still php files.
	// Add to the list, if you find out that there are other file
	// extensions that are PHP but don't end in PHP.
	if($extension == 'php' || $extension == 'inc') {
		// Get the source of the file
		$getPHP = $za->getFromIndex($i);

		$tokenizer = new WP_Version_Tokenizer($getPHP);
		$tokenizer->strip_unneeded();

		$defined = $tokenizer->get_defined();
		if( !empty($defined) )
		{
			foreach($defined as $define)
			{
				$wp_version->addConstant($define);
			}
		}

		$globals = $tokenizer->get_globals();
		if( !empty($globals) )
		{
			foreach($globals as $global)
			{
				$wp_version->addGlobal($global);
			}
		}

		$actions = $tokenizer->get_actions();
		if( !empty($actions[0]) )
		{
			$limit = count($actions[0]);
			for($z=0; $z<$limit; ++$z)
			{
				$wp_version->addAction($actions[0][$z], $actions[1][$z], $file['name']);
			}
		}

		$filters = $tokenizer->get_filters();
		if( !empty($filters[0]) )
		{
			$limit = count($filters[0]);
			for($z=0; $z<$limit; ++$z)
			{
				$wp_version->addFilter($filters[0][$z], $filters[1][$z], $file['name']);
			}
		}

		$classes = $tokenizer->get_class();
		$functions = $tokenizer->get_function();

		if( !empty($functions[0]) )
		{
			//var_dump($functions);
			$limit = count($functions[0]);
			for($z=0; $z<$limit; ++$z)
			{
				$wp_version->addFunction($functions[0][$z], $functions[1][$z]);
			}
		}

		if( !empty($classes) )
		{
			echo '<h3>'.$file['name'].'</h3>';
			foreach($classes as $class)
			{
				$classname = $class->getClassName();
				$wp_version->addClassName($classname);
				var_dump($classname);
				foreach($class->getMethods() as $method)
				{
					$wp_version->addMethod($method, $classname);
				}
				
				foreach($class->getProperties() as $property)
				{
					$wp_version->addProperty($property, $classname);
				}
			}
		}

	}
}
echo "<br /><br /><p><a href=\"process-types.php\">Complete!</a></p>";
// PHP will close this, but we are good neighbors, so we will close it instead.
$za->close();

?>